package net.lab1024.sa.admin.module.business.address.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.address.dao.AddressDao;
import net.lab1024.sa.admin.module.business.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.address.domain.form.AddressAddForm;
import net.lab1024.sa.admin.module.business.address.domain.form.AddressQueryForm;
import net.lab1024.sa.admin.module.business.address.domain.form.AddressUpdateForm;
import net.lab1024.sa.admin.module.business.address.domain.vo.AddressVO;
import net.lab1024.sa.admin.module.business.picture.dao.PictureDao;
import net.lab1024.sa.admin.module.business.picture.domain.entity.PictureEntity;
import net.lab1024.sa.admin.module.business.picture.domain.form.PictureAddForm;
import net.lab1024.sa.admin.module.business.picture.domain.vo.PictureVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

/**
 * 全景图地点添加 Service
 *
 * @Author 骆伟林
 * @Date 2025-10-23 14:52:49
 * @Copyright 软件需求工程第八组
 */

@Service
public class AddressService {

    @Resource
    private AddressDao addressDao;
    @Resource
    private PictureDao pictureDao;

    /**
     * 分页查询
     */
    public PageResult<AddressVO> queryPage(AddressQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<AddressVO> list = addressDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<Map<String, Object>> add(AddressAddForm addForm) {
        AddressEntity addressEntity = SmartBeanUtil.copy(addForm, AddressEntity.class);
        addressDao.insert(addressEntity);
        // MyBatis Plus 会自动将生成的主键填充到 addressEntity 中
        Map<String, Object> result = new HashMap<>();
        result.put("addressId", addressEntity.getAddressId());
        result.put("addressName", addressEntity.getAddressName());
        return ResponseDTO.ok(result);
    }
    /**
     * 批量添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<List<Map<String, Object>>> batchAdd(List<AddressAddForm> addFormList) {
        List<Map<String, Object>> results = new ArrayList<>();

        for (AddressAddForm addForm : addFormList) {
            AddressEntity addressEntity = SmartBeanUtil.copy(addForm, AddressEntity.class);
            addressDao.insert(addressEntity);

            // 创建返回结果Map，包含id和addressName
            Map<String, Object> result = new HashMap<>();
            result.put("addressId", addressEntity.getAddressId());
            result.put("addressName", addressEntity.getAddressName());
            results.add(result);
        }

        return ResponseDTO.ok(results);
    }

    /**
     * 更新
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(AddressUpdateForm updateForm) {
        AddressEntity addressEntity = SmartBeanUtil.copy(updateForm, AddressEntity.class);
        addressDao.updateById(addressEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }
        addressDao.batchUpdateDeleted(idList, true);
        //同时删除对应地点的图片
        pictureDao.batchUpdateDeletedByAddressId(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long addressId) {
        if (null == addressId){
            return ResponseDTO.ok();
        }
        addressDao.updateDeleted(addressId, true);
        //同时删除对应地点的图片
        pictureDao.batchUpdateDeletedByAddressId(List.of(addressId), true);
        return ResponseDTO.ok();
    }


    /**
     * 获取图片id和名称列表
     */
    public List<AddressVO> getAddressList() {
        return SmartBeanUtil.copyList(
                addressDao.selectList(new LambdaQueryWrapper<AddressEntity>()
                        .eq(AddressEntity::getDeletedFlag, false)),
                AddressVO.class);
    }

}
